package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.MessageInfo;
import edu.nf.exam.service.MessageService;
import edu.nf.exam.service.impl.MessageServiceImpl;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import java.util.List;

/**
 * @Author : LLH
 * @create 2021/6/21
 */
public class MessageController extends BaseController{
    /**
     * 查询所有消息
     * @param messageInfo
     * @return
     */
    @RequestMapping("/message/selectAll")
    public View selectAll(MessageInfo messageInfo){
        MessageService messageService=new MessageServiceImpl();
        try {
            List<MessageInfo> list = messageService.checkSelectAll(messageInfo);

            JsonView jsonView = new JsonView(success(list));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 修改
     * @param messageInfo
     * @return
     */
    @RequestMapping("/message/update")
    public View update(MessageInfo messageInfo){
        MessageService messageService=new MessageServiceImpl();
        try {
            messageService.checkUpdate(messageInfo);
            return new JsonView(success("修改成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 添加（消息发送）
     * @param messageInfo
     * @return
     */
    @RequestMapping("/message/add")
    public View add(MessageInfo messageInfo){
        MessageService messageService=new MessageServiceImpl();
        try {
            messageService.add(messageInfo);
            return new JsonView(success("发送成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 分页查询所有消息
     * @param sendUserName
     * @param receiveUserName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/message/selectByPage")
    public View selectByPage(String sendUserName, String receiveUserName, Integer page, Integer limit){
        MessageService messageService=new MessageServiceImpl();
        PageInfo<MessageInfo> pageInfo = messageService.selectByPage(sendUserName, receiveUserName, page, limit);

        JsonView jsonView = new JsonView(page(pageInfo));
        jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return jsonView;
    }
}
